<?php

class ObserController extends BackendController
{

    public $layout='/layouts/column2';
    public $city_en;
    public function actionOpen()
    {
        header("Content-type: text/html; charset=utf-8");
        $model = new Open();
        $criteria = new CDbCriteria;
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($model, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>40,
            ),
        ));
        $this->render('open',array('dataProvider'=>$dataProvider));
    }
    public function actionOpenAdd(){
        $model = new Open();
        if(Yii::app()->request->isPostRequest){
            $_POST['Open']['status'] = 0;
            $_POST['Open']['create_time'] = time();

            $_POST['Open']['start_time'] = strtotime($_POST['Open']['start_time']);
            $_POST['Open']['end_time'] = strtotime($_POST['Open']['end_time']);
            $_POST['Open']['open_time'] = intval($_POST['Open']['open_time']);



            $_POST['Open']['gid'] = implode(",",$_POST['guancha']);
            $_POST['Open']['lid'] = implode(",",$_POST['tingke']);
            $_POST['Open']['zid'] = implode(",",$_POST['zhuanjia']);

            $model->setAttributes($_POST['Open']);
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '发布公开课成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            //获取老师数据
            $techer = People::model()->findAll('role=:role',array(':role'=>3));
            $zhuanjia = Human::model()->findAll('role=:role',array(':role'=>3));
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            $this->layout = '/layouts/dialog';
            $this->render('open_add',array('techer'=>$techer,'zhuanjia'=>$zhuanjia));
        }
    }
    public function actionOpenUpdate(){
        $id = Yii::app()->request->getParam('id');
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        //获取区域
        $info = Open::model()->findByPk($id);
        if(Yii::app()->request->isPostRequest){
            if($info){

                $_POST['Open']['start_time'] = strtotime($_POST['Open']['start_time']);
                $_POST['Open']['end_time'] = strtotime($_POST['Open']['end_time']);
                $_POST['Open']['open_time'] = intval($_POST['Open']['open_time']);



                $_POST['Open']['gid'] = implode(",",$_POST['guancha']);
                $_POST['Open']['lid'] = implode(",",$_POST['tingke']);
                $_POST['Open']['zid'] = implode(",",$_POST['zhuanjia']);

                $info->setAttributes($_POST['Open']);
                if($info->update()){
                    //写入周边
                    $data['status'] = 1;
                    $data['message'] = '公开课修改成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = $this->showError($info->getErrors());
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你无权作此操作';
            }

            echo json_encode($data);
            Yii::app()->end();
        }

        $techer = People::model()->findAll('role=:role',array(':role'=>3));
        $zhuanjia = Human::model()->findAll('role=:role',array(':role'=>3));
        $info->start_time = date('Y-m-d H:i:s',$info->start_time);
        $info->end_time = date('Y-m-d H:i:s',$info->end_time);
        $this->layout = '/layouts/dialog';
        $this->render('open_update',array('techer'=>$techer,'zhuanjia'=>$zhuanjia,'info'=>$info));
    }
    public function actionOpenDelete(){
        $id = Yii::app()->request->getParam('id');
        $info = Open::model()->findByPk($id);
        if($info){
            $ok = $info->delete();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '删除数据成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '删除数据失败';
            }

        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }
}